package com.timmy.test1.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.appcompat.widget.AppCompatTextView;

import com.blankj.utilcode.util.LogUtils;
import com.timmy.test1.common.JiActivityVKeyHelper;

/**
 * Created by timmy on 2020/5/7.
 */
public class CustomKeyView extends AppCompatTextView implements View.OnTouchListener {


    protected float continuousFrequency = 0.1F;

    private int endX;

    private int endY;

    protected boolean isContinuous;

    private boolean isFling;

    protected boolean isLongPress;

    private boolean isMoved = false;

    protected int keyType;

    private int lastX;

    private int lastY;

    private FrameLayout.LayoutParams layoutParams;

    public int leftMargin;

    public boolean mCanTouchMove = true;

    private OnCusKeyViewClickListener mLister;

    private OnCusKeyViewMoveListener mMoveListener;

    public int mViewHeight;

    public int mViewWidth;

    protected int rockType;

    protected int scanCode;

    private int screenHeight;

    private int screenWidth;

    protected int size = 6;

    private int startX;

    private int startY;

    public int topMargin;

    public float vHeight;

    public float vWidth;

    public CustomKeyView(Context paramContext) {
        super(paramContext);
        init();
    }

    public CustomKeyView(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        init();
    }

    public CustomKeyView(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
        init();
    }

    private void getWH() {
        this.screenHeight = JiActivityVKeyHelper.mActivityHeight;
        this.screenWidth = JiActivityVKeyHelper.mActivityWidth;
    }

    public float getContinuousFrequency() {
        return this.continuousFrequency;
    }

    public int getKeyType() {
        return this.keyType;
    }

    public int getLeftMargin() {
        return this.leftMargin;
    }

    public int getRockType() {
        return this.rockType;
    }

    public int getScanCode() {
        return this.scanCode;
    }

    public int getSize() {
        return this.size;
    }

    public int getTopMargin() {
        return this.topMargin;
    }

    public void init() {
        setOnTouchListener(this);
        getWH();
        post(new Runnable() {
            @Override
            public void run() {
                CustomKeyView customKeyView = CustomKeyView.this;
                customKeyView.mViewHeight = customKeyView.getHeight();
                customKeyView = CustomKeyView.this;
                customKeyView.mViewWidth = customKeyView.getWidth();
                customKeyView = CustomKeyView.this;
                customKeyView.leftMargin = customKeyView.getLeft();
                customKeyView = CustomKeyView.this;
                customKeyView.topMargin = customKeyView.getTop();
            }
        });
    }

    public boolean isContinuous() {
        return this.isContinuous;
    }

    public boolean isFling() {
        return this.isFling;
    }

    public boolean isLongPress() {
        return this.isLongPress;
    }

    @Override
    public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
        this.mViewHeight = getHeight();
        this.mViewWidth = getWidth();
        int i = paramMotionEvent.getAction();

        int action = paramMotionEvent.getAction();
        if (action != MotionEvent.ACTION_DOWN) {
            if (action != MotionEvent.ACTION_UP) {
                if (action != MotionEvent.ACTION_MOVE) {
                    return true;
                }
                if (!this.mCanTouchMove) {
                    return true;
                }
                this.isMoved = true;
                i = (int) paramMotionEvent.getRawX() - this.lastX;
                int j = (int) paramMotionEvent.getRawY() - this.lastY;
                this.leftMargin = paramView.getLeft() + i;
                this.topMargin = paramView.getTop() + j;
                i = paramView.getRight() + i;
                int k = paramView.getBottom() + j;
                if (this.leftMargin < 0) {
                    this.leftMargin = 0;
                    i = 0 + this.mViewWidth;
                }
                int m = this.screenWidth;
                j = i;
                if (i > m) {
                    this.leftMargin = m - this.mViewWidth;
                    j = m;
                }
                i = k;
                if (this.topMargin < 0) {
                    this.topMargin = 0;
                    i = this.mViewHeight + 0;
                }
                m = this.screenHeight;
                k = i;
                if (i > m) {
                    this.topMargin = m - this.mViewHeight;
                    k = m;
                }
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("custom move:");
                stringBuilder.append(this.leftMargin);
                stringBuilder.append(" ");
                stringBuilder.append(this.topMargin);
                Log.e("save", stringBuilder.toString());

                // 这里是移动view
                paramView.layout(this.leftMargin, this.topMargin, j, k);

                this.lastX = (int) paramMotionEvent.getRawX();
                this.lastY = (int) paramMotionEvent.getRawY();

                OnCusKeyViewMoveListener onCusKeyViewMoveListener = this.mMoveListener;
                if (onCusKeyViewMoveListener != null) {
                    onCusKeyViewMoveListener.onMove(this.leftMargin, this.topMargin);
                    return true;
                }
            } else {
                if (this.isMoved) {
                    this.layoutParams = (FrameLayout.LayoutParams) getLayoutParams();
                    StringBuilder stringBuilder2 = new StringBuilder();
                    stringBuilder2.append("custom a:");
                    stringBuilder2.append(this.leftMargin);
                    stringBuilder2.append(" ");
                    stringBuilder2.append(this.topMargin);
                    Log.e("save", stringBuilder2.toString());
                    FrameLayout frameLayout = (FrameLayout) getParent();
                    int i1 = frameLayout.getChildCount();
                    int k = 0;
                    int n = -1;
                    int m = 0;
                    int j = -1;
                    int i4 = 0;


                    int leftMargin = -1;
                    int lineWidth = 0;


                    for (i = 0; k < i1; i = i4) {
                        View view = frameLayout.getChildAt(k);
                        boolean bool = view instanceof SupportLineView;
                        int i3 = n;
                        int i2 = m;
                        if (bool) {
                            SupportLineView supportLineView = (SupportLineView) view;
                            i3 = n;
                            i2 = m;
                            if (supportLineView.getOrientation() == 1) {
                                i3 = supportLineView.getLeftMargin();
                                i2 = view.getWidth();
                            }
                        }
                        int i5 = j;
                        i4 = i;
                        if (bool) {
                            SupportLineView supportLineView = (SupportLineView) view;
                            i5 = j;
                            i4 = i;
                            if (supportLineView.getOrientation() == 2) {
                                i5 = supportLineView.getTopMargin();
                                i4 = view.getHeight();
                            }
                        }
                        k++;
                        n = i3;
                        m = i2;
                        j = i5;

                        leftMargin = i3;
                        lineWidth = i2;
                    }

                    if (leftMargin != -1) {
                        LogUtils.i("n: " + n + " m: " + m);

                        int lineCenterPointX = leftMargin + lineWidth / 2;

                        k = n + m / 2;

                        int viewLeftMargin = this.leftMargin;
                        int viewWidth = this.mViewWidth;

                        int viewCenterPointX = viewLeftMargin + viewWidth / 2;

                        // 先判断左右
                        if (viewCenterPointX <= lineCenterPointX) {
                            // 左边
                            if (lineCenterPointX - viewCenterPointX <= mViewWidth / 2 || lineCenterPointX - (viewLeftMargin + mViewWidth) <= 50) {
                                this.leftMargin = lineCenterPointX - mViewWidth;
                            }

                        } else {
                            // 右边
                            if (viewCenterPointX - lineCenterPointX < mViewWidth / 2 || viewLeftMargin - lineCenterPointX <= 50) {
                                this.leftMargin = lineCenterPointX;
                            }
                        }
                    }

                    int supportLinePointY = j;
                    if (supportLinePointY != -1) {
                        i = j + i / 2;
                        j = this.topMargin;
                        int viewCenterPointY = topMargin + mViewHeight / 2;
                        int linePointY = i;
                        if (viewCenterPointY <= linePointY) {
                            if (supportLinePointY - viewCenterPointY <= (mViewHeight / 2) || supportLinePointY - topMargin + mViewHeight <= 50) {
                                this.topMargin = linePointY - mViewHeight;
                            }
                        } else {
                            if (j - i >= -(this.mViewHeight / 2) && j - i < 50) {
                                this.topMargin = linePointY;
                            }
                        }
                    }
                    this.layoutParams.leftMargin = this.leftMargin;
                    this.layoutParams.topMargin = this.topMargin;
                    StringBuilder stringBuilder1 = new StringBuilder();
                    stringBuilder1.append("custom b:");
                    stringBuilder1.append(this.leftMargin);
                    stringBuilder1.append(" ");
                    stringBuilder1.append(this.topMargin);
                    Log.e("save", stringBuilder1.toString());
                    setLayoutParams((ViewGroup.LayoutParams) this.layoutParams);
                }

                this.endX = (int) paramMotionEvent.getRawX();
                this.endY = (int) paramMotionEvent.getRawY();
                if (Math.abs(this.startX - this.endX) < 1 && Math.abs(this.startY - this.endY) < 1) {
                    return false;
                }
                OnCusKeyViewMoveListener onCusKeyViewMoveListener = this.mMoveListener;
                if (onCusKeyViewMoveListener != null) {
                    onCusKeyViewMoveListener.onDown(this.endX, this.endY);
                    return true;
                }
            }
        } else {
            this.lastX = (int) paramMotionEvent.getRawX();
            i = (int) paramMotionEvent.getRawY();
            this.lastY = i;
            int j = this.lastX;
            this.startX = j;
            this.startY = i;
            OnCusKeyViewMoveListener onCusKeyViewMoveListener = this.mMoveListener;
            if (onCusKeyViewMoveListener != null) {
                onCusKeyViewMoveListener.onDown(j, i);
            }
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent paramMotionEvent) {
        if (this.mLister != null) {
            this.mLister.onCusKeyViewClick(this);
        }
        return true;
    }

    public void setContinuous(boolean paramBoolean) {
        this.isContinuous = paramBoolean;
    }

    public void setContinuousFrequency(float paramFloat) {
        this.continuousFrequency = paramFloat;
    }

    public void setFling(boolean paramBoolean) {
        this.isFling = paramBoolean;
    }

    public void setKeyType(int paramInt) {
        this.keyType = paramInt;
    }

    public void setLeftMargin(int paramInt) {
        this.leftMargin = paramInt;
    }

    public void setLongPress(boolean paramBoolean) {
        this.isLongPress = paramBoolean;
    }

    public void setOnCusKeyViewClickListener(OnCusKeyViewClickListener paramOnCusKeyViewClickListener) {
        this.mLister = paramOnCusKeyViewClickListener;
    }

    public void setOnMoveListener(OnCusKeyViewMoveListener paramOnCusKeyViewMoveListener) {
        this.mMoveListener = paramOnCusKeyViewMoveListener;
    }

    public void setRockType(int paramInt) {
        this.rockType = paramInt;
    }

    public void setScanCode(int paramInt) {
        this.scanCode = paramInt;
    }

    public void setSize(int paramInt) {
        this.size = paramInt;
    }

    public void setTopMargin(int paramInt) {
        this.topMargin = paramInt;
    }

    public static interface OnCusKeyViewClickListener {
        void onCusKeyViewClick(CustomKeyView param1CustomKeyView);
    }

    public static interface OnCusKeyViewMoveListener {
        void onDown(int param1Int1, int param1Int2);

        void onMove(int param1Int1, int param1Int2);

        void onUp(int param1Int1, int param1Int2);
    }
}
